package com.qingkai.onlineshop.presentation.dashboard

import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.SuggestionChip
import androidx.compose.material3.SuggestionChipDefaults
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment.Companion.CenterVertically
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.qingkai.onlineshop.app.Route
import com.qingkai.onlineshop.data.entity.CategoryEntity

/**
 *作者：created by 89270 on 2025/6/9 15:39
 *描述：
 */
@Composable
fun CategoryList(
    categories: List<CategoryEntity>,
    modifier: Modifier,
    navigateToRoute: (Route) -> Unit
) {
    var selected by remember { mutableIntStateOf(0) }
    LazyRow(
        modifier = modifier,
        verticalAlignment = CenterVertically,
        horizontalArrangement = Arrangement.spacedBy(16.dp)
    ) {
        item { Spacer(Modifier) }
        items(categories) {
            SuggestionChip(
                onClick = {
                    selected = it.id
                    navigateToRoute.invoke(Route.Product(categoryId = it.id, title = it.title))
                },
                label = { Text(text = it.title) },
                shape = CircleShape,
                colors = SuggestionChipDefaults.suggestionChipColors(
                    containerColor =
                        if (selected == it.id) MaterialTheme.colorScheme.primary
                        else MaterialTheme.colorScheme.primaryContainer,
                    labelColor = if (selected == it.id) Color.White
                    else MaterialTheme.colorScheme.onPrimaryContainer
                ), border = BorderStroke(0.dp, Color.Transparent)
            )
        }
        item { Spacer(Modifier) }
    }
}

@Preview
@Composable
fun CategoryListPreView() {
    CategoryList(
        categories = testCategories, modifier = Modifier
            .fillMaxWidth()
            .padding(vertical = 16.dp), navigateToRoute = {}
    )
}

val testCategories = List(4) { CategoryEntity(it, "Item $it") }